var kuai3Ai = function() {
    ai.call(this);
};
kuai3Ai.prototype = Object.create(ai.prototype);

// 记录到受影响的数字
kuai3Ai.prototype.insertToNumber = function(list) {
    // 记录到各受影响的号码
    var result = [];
    for (var i = 0; i < 3; i++) {
        result[i] = {
            index: i + 1,
            total: 0,
            bet: [],
        };
    }
    for (var i in list) {
        var k = list[i].key;
        if (k.startsWith("军") || k == '大' || k == '小' || k == '豹' || k.startsWith('围')) {
            // 1 - 6
            for (var j = 0; j < 3; j++) {
                result[j].bet.push(k);
                result[j].total += list[i].amount;
            }
        }
    }
    result.sort(function(a, b) {
        return b.total - a.total;
    });
    return result;
}

// 检测某个投注项，并应用概率
kuai3Ai.prototype.apply = function(key, key2, arr1, arr2, all) {
    var self = this;
    if (key != key2) return null;
    if (this.checkRatio(self.rate)) {
        all = self.prior(all, arr1);
    }
    else {
        all = self.prior(all, arr2);
    }
    return all;
}

// 应用总和的概率
kuai3Ai.prototype.he = function(result, index, he, he2) {
    var self = this;
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, he);
    }
    if (!result[2].pre) result[2].pre = [he2];
    else result[2].pre.push(he2);

    if (he2 == '大') {
        return [4, 5, 6];
    }
    if (he2 == '小') {
        return [1, 2, 3];
    }
}

// 取得总和的开奖数据
kuai3Ai.prototype.heNumbers = function(index, he, ns) {
    var total = 0;
    var arr = [];
    for (var i = 0; i < 3; i++) {
        if (i + 1 == index) continue;
        total += ns[i];
    }
    if (he == '大') {
        for (var i = 1; i <= 6; i++) {
            if (total + i >= 11) arr.push(i);
        }
    }
    if (he == '小') {
        for (var i = 1; i <= 6; i++) {
            if (total + i < 11) arr.push(i);
        }
    }
    return arr;
}

// 军1-6
kuai3Ai.prototype.jun = function(result, index, jun) {
    var self = this;
    var n = parseInt(jun.substring(1));
    var arr = [];
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, jun);
    }

    if (self.checkRatio(self.rate)) {
        if (!result[1].pre) result[1].pre = [jun];
        else result[1].pre.push(jun);
        if (!result[2].pre) result[2].pre = [jun];
        else result[2].pre.push(jun);

        for (var i = 0; i <= 6; i++) if (i != n) arr.push(i);
        return arr;
    }
    return [1, 2, 3, 4, 5, 6];
}
kuai3Ai.prototype.junNumbers = function(index, jun, ns) {
    var self = this;
    var n = parseInt(jun.substring(1));
    var arr = [];
    for (var i = 0; i <= 6; i++) if (i != n) arr.push(i);
    return arr;
}

// 围骰
kuai3Ai.prototype.bao = function(result, index) {
    var self = this;
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, '豹');
    }
    if (self.checkRatio(self.rate)) {
        if (!result[2].pre) result[2].pre = ['豹'];
        else result[2].pre.push('豹');
    }
}
kuai3Ai.prototype.baoNumbers = function(index, ns) {
    var self = this;
    var arr = [1, 2, 3];
    self.deleteItem(arr, index);
    if (ns[arr[0] - 1] != ns[arr[1] - 1]) return [1, 2, 3, 4, 5, 6];
    else {
        var arr2 = [1, 2, 3, 4, 5, 6];
        self.deleteItem(arr2, ns[arr[0] - 1]);
        return arr2;
    }
}

// 入口
kuai3Ai.prototype.open = function(bets, lottery, rate) {
    var self = this;
    self.map = lottery;
    self.rate = rate;
    
    // 合并下数据
    var arr = [["大", "小"]];
    var list = self.mixBets(bets, arr);

    // 记录到各受影响的号码
    var result = self.insertToNumber(list);

    // 逐个号码开出来
    var numbers = [1, 2, 3, 4, 5, 6];
    var lottery = [];
    var ns = [];
    for (var i in result) {
        var index = result[i].index;
        var bets = result[i].bet;
        if (self.isEmpty(bets) && self.isEmpty(result[i].pre)) continue;

        var all = numbers.concat([]);
        if (result[i].pre) {
            // 这些受其他号码影响，需要优先处理
            for (var j in result[i].pre) {
                var k = result[i].pre[j];
                if (k == '大' || k == '小') {
                    all = self.prior(all, self.heNumbers(index, k, ns));
                }
                else if (k.startsWith("军")) {
                    all = self.prior(all, self.junNumbers(index, k, ns));
                }
                else if (k == '豹') {
                    all = self.prior(all, self.baoNumbers(index, ns));
                }
            }
        }
        for (var j in bets) {
            var k = bets[j];
            if (!k) continue;
            var k2 = k.substring(1);
            do {
                if (k == '大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '小'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                }
                else if (k == '小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '大'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                }
                else if (k.startsWith("军")) {
                    all = self.prior(all, self.jun(result, index, k));
                }
                else if (k.startsWith("围")) {
                    var nn = parseInt(k2);
                    var temp = [];
                    for (var tt = 1; tt <= 6; tt++) if (tt != nn) temp.push(tt);
                    all = self.prior(all, temp);
                }
                else if (k == '豹') {
                    self.bao(result, index);
                }
            } while (false);
            if (all.length == 1) break;
        }

        // 应用概率，抽取一个出来
        ns[index - 1] = self.random(all);
    }

    // 还没有开出的号码，随机开了
    for (var i = 0; i < 3; i++) {
        if (ns[i] !== undefined) continue;
        ns[i] = self.random(numbers);
    }

    // 返回开奖结果
    return ns;
}
var Kuai3Ai = new kuai3Ai();
